package com.straw.hat.com.straw.hat.jdk.concurrent.executor.exception;

import java.io.IOException;
import java.util.concurrent.*;

/**
 * company: 鲸灵科技
 * author: qiyue@webuy.ai
 * date: 2019/9/28
 * time: 4:28 PM
 */
public class ExecutorExceptionStudy {


    public static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(16, 50, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(20));






    public static void main(String[] args) {
        /**execute 主线程能拿到异常 异常类型和信息的就是原始的信息*/
//        THREAD_POOL_EXECUTOR.execute(new RunnableCls());
//        System.out.println("主线成开始阻塞");
//        try {
//            System.in.read();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        /**submit 主线程在不get情况不能获取到异常*/
        Future<Integer> future =   THREAD_POOL_EXECUTOR.submit(new RunnableTst());
        /**submit 主线程在get情况能获取到异常 类型是ExecutionException 错误信息是我们抛出的*/
        try {
            future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }



    static class RunnableCls implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(100);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(1/0);
        }
    }


    static class RunnableTst implements Callable<Integer> {



        @Override
        public Integer call() throws Exception {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1/0;
        }
    }



}
